import json
import os

from constants import consts
from logger import logger
from sub_events.base_event import BaseEvent

LOGGER = logger.CustomLogger()

class ListCommands(BaseEvent):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.event_name_zh: str = "查看命令列表"
        self.event_name_en: str = "Show Commands List"

    def execute(self) :
        project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        json_file = os.path.join(project_root, 'code_spectra', 'manifest.json')
        with open(json_file, 'r', encoding='utf-8') as f:
            hook_cfg = json.load(f)
            all_commands = []
            for command, cmd_json in hook_cfg.get(consts.HOOK_NAME_NOTE, {}).get("comment", {}).items():
                if cmd_json.get("internal"):
                    continue
                all_commands.append({
                    "command": command,
                    "duty": "%s(%s)" % (cmd_json.get("event_name_zh"), cmd_json.get("event_name_en"))
                })
            self.comment = ("命令清单如下，部分命令需要特定人员才可触发。\n"
                            "The command list is as follows. Some commands require specific users to be triggered.\n")
            self.comment += self.generate_html_table(all_commands, caption="命令清单(Command List)")
